/**
 * Form.js
 *
 * @description :: TODO: You might write a short summary of how this model works and what it represents here.
 * @docs        :: http://sailsjs.org/#!documentation/models
 */

module.exports = {

	attributes : {

		name : {
			type : 'string',
			required : true
		},

		type : {
			model : 'FormType',
			required : true
		},

		/*
	 	0 : "未提交",
		1 : "已拒绝",
		2 : "审批中",
		3 : "已完成",
		*/
		state : {
			type : 'integer',
			defaultsTo : 0
		},

		description : {
			type : 'string'
		},
		
		owner : {
			model : 'User',
			required : true
		},
		
		lastChecker : {
			type : 'string'
		},
		
		waitChecker : {
			type : 'string'
		},
		
		opinions : {
			collection : 'FormOpinion',
			via : 'form'
		},
		
		propvals : {
			collection : 'FormVal',
			via : 'form'
		},
		
		note : {
			model : 'LeaveNote'
		},
		
		attachments : {
			collection : 'WechatFile',
			via : 'form'
		}

	},

	afterDestroy: function(forms, cb){
		
		var count = forms.length;
		for (var i = 0; i < forms.length; i++) {
			FormVal.destroy({'form' : forms[i].id})
			.then(function (result) {
				count--;
				if(count == 0)cb();
			}).catch(function(err){
				sails.log.error(err);
				cb(err);
			});
		}
	},

};
